package com.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class FileStore {

	private static String filePath = "C:/dev/db.txt";
	private static Properties p;
	private static FileStore f = null;
	
	private FileStore(){
		
	}
	
	public static FileStore getInstance(){
		if(f == null){
			
			init();
			
		}
		return f;
	}
	
	public static void init(){
		p = new Properties();
		f = new FileStore();
		try {
			FileInputStream fs = new FileInputStream(filePath);
			p.load(fs);
			fs.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void save (String key, String value){
		FileOutputStream fo;
		try {
			fo = new FileOutputStream(filePath);
			System.out.println("key:" + key + " value: " + value + " " + p);
			p.setProperty(key, value);
			p.store(fo, "");
			fo.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String get(String key){
		return p.getProperty(key);
	}
	
	public String getAll(){
		return p.toString();
	}
	
	public static void main(String [] args){
		FileStore f = FileStore.getInstance();
		f.save("asd", "sdfd");
		System.out.println(f.getAll());
	}
}
